/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.info.service.plugins  
 */
package com.apache.info.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.manager.InfoReleaseManager;
import com.apache.util.ConfigUtil;

/**
 * description:  动态修改表操作
 * @author Hou Dayu 创建时间：2015-8-28  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class DynamicUpdateTablePluginImpl implements PluginConnector {

	private InfoReleaseManager infoReleaseManager;
	private IDao pubMetadataDao;

	/**
	 * TODO 动态修改表插件实现.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo vo) throws Exception {
		String key = vo.getMethodKey();
		String sortId = String.valueOf(vo.getParams("sortId"));
		String cacheKey = "pubSortInfo_" + sortId;
		MethodParam param = new MethodParam("ById", cacheKey, "", PubSortInfo.class.getName());
		param.setParams("sortId", sortId);
		PubSortInfo sort = (PubSortInfo) pubMetadataDao.selectSingle(param);
		if ("1".equals(sort.getSortStatus())) {
			ParamsVo pvo = new ParamsVo();
			PubMetadata metadata = (PubMetadata) vo.getParams("metadata");
			if (Validator.isNull(key) || "update".equals(key)) {//添加新字段
				String sql = getSql(sort.getInfoTableName(), metadata);
				pvo.setParams("dyncSql", sql);
				return infoReleaseManager.editInfo(pvo);
			} else if ("del".equals(key)) {//删除字段
				String sql = "ALTER TABLE " + sort.getInfoTableName() + " DROP COLUMN " + metadata.getDataShortName();
				pvo.setParams("dyncSql", sql);
				return infoReleaseManager.deleteInfo(pvo);
			} else if ("modify".equals(key)) {//修改字段长度
				PubMetadata old = (PubMetadata) vo.getParams("old");
				String sql = modifySql(sort.getInfoTableName(), metadata, old);
				if (Validator.isNotNull(sql)) {
					pvo.setParams("dyncSql", sql);
					return infoReleaseManager.editInfo(pvo);
				}
			}
		}
		return true;
	}

	private String modifySql(String tableName, PubMetadata pm, PubMetadata old) {
		StringBuffer sql = new StringBuffer("ALTER TABLE " + tableName);
		String dbType = ConfigUtil.getInstance().getValueByKey("jdbc.properties", "database_type");
		if (old.getDataShortName().equalsIgnoreCase(pm.getDataShortName())) {
			sql.append(" modify " + pm.getDataShortName());
			if ("1".equals(pm.getDataType()) || "2".equals(pm.getDataType())) {
				if ("oracle".equals(dbType)) {
					sql.append(" number(" + pm.getDataLength() + ") ");
				} else {
					return "";
				}
			} else if ("3".equals(pm.getDataType())) {
				if ("oracle".equals(dbType)) {
					sql.append(" number(" + pm.getDataLength() + ",2) ");
				} else {
					return "";
				}
			} else if ("4".equals(pm.getDataType())) {
				if ("oracle".equals(dbType)) {
					sql.append(" char(" + pm.getDataLength() + ") ");
				} else {
					sql.append(" char(" + pm.getDataLength() + ") ");
				}
			} else if ("6".equals(pm.getDataType())) {
				return "";
			} else {
				if ("oracle".equals(dbType)) {
					sql.append(" varchar2(" + pm.getDataLength() + ") ");
				} else {
					sql.append(" varchar(" + pm.getDataLength() + ") ");
				}
			}
		} else {
			sql.append(" rename column " + old.getDataShortName() + " to " + pm.getDataShortName());
		}
		return sql.toString();
	}

	private String getSql(String tableName, PubMetadata pm) {
		StringBuffer sql = new StringBuffer("ALTER TABLE " + tableName + " ADD " + pm.getDataShortName() + " ");
		String dbType = ConfigUtil.getInstance().getValueByKey("jdbc.properties", "database_type");
		if ("1".equals(pm.getDataType()) || "2".equals(pm.getDataType())) {
			if ("oracle".equals(dbType)) {
				sql.append(" number(" + pm.getDataLength() + ") ");
			} else {
				if ("1".equals(pm.getDataType())) {
					sql.append(" int ");
				} else if ("2".equals(pm.getDataType())) {
					sql.append(" bigint ");
				}
			}
		} else if ("3".equals(pm.getDataType())) {
			if ("oracle".equals(dbType)) {
				sql.append(" number(" + pm.getDataLength() + ",2) ");
			} else {
				sql.append(" float ");
			}
		} else if ("4".equals(pm.getDataType())) {
			sql.append(" char(" + pm.getDataLength() + ") ");
		} else if ("6".equals(pm.getDataType())) {
			if ("oracle".equals(dbType)) {
				sql.append(" date ");
			} else {
				sql.append(" datetime ");
			}
		} else {
			if ("oracle".equals(dbType)) {
				sql.append(" varchar2(" + pm.getDataLength() + ") ");
			} else {
				sql.append(" varchar(" + pm.getDataLength() + ") ");
			}
		}

		if ("1".equals(pm.getDataRestrict())) {
			sql.append(" not null ");
		} else if ("2".equals(pm.getDataRestrict())) {
			sql.append(" not null ");
		}
		return sql.toString();
	}

	public void setInfoReleaseManager(InfoReleaseManager infoReleaseManager) {
		this.infoReleaseManager = infoReleaseManager;
	}

	public void setPubMetadataDao(IDao pubMetadataDao) {
		this.pubMetadataDao = pubMetadataDao;
	}

}
